Bite 24. ABC's and class inheritance
Login and get coding
ABC's orAbstract Base Classesare great to enforce a common API for your subclasses.You define one or more methods and/or properties as abstract in the base class, and if the subclass does not implement them it raises a
TypeError. In this bite you will use this concept as follows:
- Define a Challenge base class that inherits from
ABC(given), its constructor receives anumberand atitleattribute.- On Challenge define an
abstractmethodcalledverifyand aproperty(< 3.3 it would be anabstractproperty) calledpretty_title.- Create the
BlogChallengeandBiteChallengeclasses which both inherit fromChallenge. Note that they would raise aTypeErrorat this point, exactly what you want: enforcing the use of the abstract method/ property.BlogChallengeandBiteChallenge's constructors call the parent constructor (don't worry it's supercool, remember: we use Python3 so adjust your syntax), and both receive an extra argument in the constructor:merged_prsforBlogChallengeandresultforBiteChallenge.- Implement the required methods and properties, refer to the tests what they need to return.
Get coding, learn more about classes, and have fun!
257 out of 262 users completed this Bite.
Will you be the 258th person to crack this Bite?
Resolution time: ~50 min. (avg. submissions of 5-240 min.)
Our community rates this Bite 4.93 on a 1-10 difficulty scale.
» Up for a challenge? 💪